package com.qingcheng.service.impl;

import com.qingcheng.dao.StatisticsMapper;
import com.qingcheng.pojo.order.Statistics;
import com.qingcheng.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired(required = false)
    private StatisticsMapper statisticsMapper;

    // 定时插入昨天的订单交易情况
    @Override
    public Statistics deal(String date1, String date2) {
        Date date = new Date();
        // 创建一个新的对象
        Statistics s=new Statistics();
        // 查询出订单数，订单单价数,总额,订单人数
        HashMap<String, Long> map=statisticsMapper.findOrder(date1,date2);
        // 查询出付款人数,付款订单数,付款件数,订单金额
        HashMap<String, Long> map1 = statisticsMapper.PayOrder(date1, date2);
        // 设置浏览记录
        s.setBrowseSum(15);
        // 设置id的
        s.setCreateDate(date);
        // 判断是否有下单人数
        if(map.get("place_sum")==null ){
            return null;
        }
        s.setPlaceSum(map.get("place_sum").intValue());
        // 订单数
        s.setOrderSum(map.get("order_sum").intValue());
        // 订单件数
        s.setSingleNumber(map.get("single_number").intValue());
        //  下单金额
        Object o1 = map.get("money_amount");
        Integer ma= Integer.parseInt(String.valueOf(o1 ));
        Float money_amount= ma.floatValue();
        s.setMoneyAmount(money_amount.floatValue());
        // 查询有效订单数并设置值
        s.setValidSum(statisticsMapper.Order(date1,date2));
        // 退款金额
        s.setReturnMoney(statisticsMapper.Retrun_Money(date1, date2));
        // 付款人数(待修改)
        s.setPayPersonsum(map1.get("pay_personsum").intValue());
        // 付款订单
        s.setPayOrdersum(map1.get("pay_ordersum").intValue());
        // 付款件数
        s.setPaySum(map1.get("pay_sum").intValue());
        // 付款金额
        Object o2= map1.get("pay_money");
        Integer pm= Integer.parseInt(String.valueOf(o2 ));
        Float pay_money= pm.floatValue();
        s.setPayMoney(pay_money.floatValue());
        // 客单价
        s.setUnitPrice(pay_money.floatValue()/map1.get("pay_personsum").intValue());

        // 添加统计表格数据
        statisticsMapper.insertSelective(s);
        return s;
    }

    // 查询订单统计记录
    @Override
    public Statistics dealfind(Map<String,String> map) {
        Example example=new Example(Statistics.class);
        Example.Criteria cr=example.createCriteria();
        cr.andGreaterThanOrEqualTo("createDate",map.get("date1"));
        cr.andLessThan("createDate",map.get("date2"));
        Statistics statistics =  statisticsMapper.dealfind(map.get("date1"),map.get("date2"));
        return statistics;
    }

    @Override
    public List<HashMap> goodsfind(Map<String,String> map) {
        List<HashMap> h=statisticsMapper.GoodsFind(map.get("date1"),map.get("date2"));
        return h;
    }

    // 订单来源的构成
    @Override
    public List<HashMap> orderSource(LocalDate date1,LocalDate date2) {
        if(date1==null || date2==null){
            date1=LocalDate.of(2019,04,01);
            date2=LocalDate.of(2019,05,01);
        }
        List<HashMap> map=statisticsMapper.orderSource(date1,date2);
        return map;
    }

    // 来源比较
    @Override
    public List<HashMap> SourceComparison() {
        LocalDate date1=LocalDate.of(2019,05,01);
        LocalDate date2=LocalDate.of(2019,06,01);
        List<HashMap> map=statisticsMapper.orderSource(date1,date2);
        return map;
    }

    private Example createExample(Map map) {
        Example example=new Example(Statistics.class);
        Example.Criteria cr=example.createCriteria();
        if(map!=null){
            if(map.get("date1")==null || "".equals(map.get("date1"))){
                cr.andGreaterThanOrEqualTo("createDate",map.get("date1"));
            }
            if(map.get("date2")==null || "".equals(map.get("date2"))){
                cr.andLessThanOrEqualTo("createDate",map.get("date1"));
            }
        }
        return example;
    }
}
